package com.hxf.common;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;

/**
 * 日期工具类
 * 
 * @author tengyicheng
 * @version [1.0, 2013/09/22]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class DateUtil {
	// 初始化日志

	public static final String DATE_PATTERN_M = "yyyy-MM-dd HH:mm:ss";

	public static final String DATE_PATTERN_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";

	public static final String DATE_PATTERN_D = "yyyy-MM-dd";
	
	public static final String DATE_PATTERN_Y = "yyyyMM";

	public static final String DATE_PATTERN_D_CN = "yyyy年MM月dd日";
	
	public static final String DATE_PATTERN_Dy = "MM/dd/yyyy";

	public static Date format(String dateStr) {
		Date date = null;
		if (dateStr == null || "".equals(dateStr.trim())) {
			return date;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN_Dy);

		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			date = null;
		}
		return date;
	}

	/**
	 * 把字符串转化为日期类型
	 * 
	 * @param dateStr
	 * @param pattern
	 * @return
	 */
	public static Date convert2Date(String dateStr, String pattern) {
		Date date = null;
		if (dateStr == null || "".equals(dateStr.trim())) {
			return date;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			date = null;
		}
		return date;
	}
	
	public static String format(Date date) {
		String dateStr = null;
		if (date == null) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN_Dy);
		dateStr = sdf.format(date);
		return dateStr;
	}

	/**
	 * 根据日期模式，返回需要的日期对象
	 * 
	 * @param date
	 * @param pattern
	 * @return String
	 */
	public static String format(Date date, String pattern) {
		String dateStr = null;
		if (date == null) {
			return null;
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		dateStr = sdf.format(date);
		return dateStr;
	}

	/**
	 * 根据日期模式, 获取当前日期
	 * 
	 * @param pattern
	 * @return
	 */
	public static String getCurrentDate(String pattern) {
		return format(new Date(), pattern);
	}
	
	/**
	 * 获取DATE_PATTERN_M格式日期
	 * @return
	 */
	public static String getDatePatternM() {
		return format(new Date(), DATE_PATTERN_M);
	}

	/**
	 * 日期加一天的方法
	 * 
	 * @param date
	 * @return
	 */
	public static Date getDateAddOne(Date date) {
		if (date == null) {
			return null;
		}
		Calendar c = Calendar.getInstance();
		// 设置日期
		c.setTime(date);
		// 日期加1
		c.add(Calendar.DATE, 1);
		// 结果
		return c.getTime();
	}

	/**
	 * 当前系统下一个月份
	 * 
	 * @return
	 */
	public static Long getNextDate() {
		Calendar cal = Calendar.getInstance();
		cal.add(GregorianCalendar.MONTH, 1);
		SimpleDateFormat theDate = new SimpleDateFormat(DATE_PATTERN_Y);
		String dateString = theDate.format(cal.getTime());
		return Long.valueOf(dateString);
	}
	
	/**
     * 获取当前日期是星期几<br>
     *
     * @param dt
     * @return 当前日期是星期几
     */
    public static String getWeekOfDate(Date dt) {
        String[] weekDays = {"星期日", "星期一", "星期二", "星期三", "星期四", "星期五", "星期六"};
        Calendar cal = Calendar.getInstance();
        cal.setTime(dt);

        int w = cal.get(Calendar.DAY_OF_WEEK) - 1;
        if (w < 0)
            w = 0;

        return weekDays[w];
    }
    
    public static String createDateFolder(Date time) {
		SimpleDateFormat formatYear = new SimpleDateFormat("yyyy");
		SimpleDateFormat formatMonth = new SimpleDateFormat("MM");
		SimpleDateFormat formatDay = new SimpleDateFormat("dd");

		return formatYear.format(time) + "/" + formatMonth.format(time) + "/"
				+ formatDay.format(time) + "/";
	}
    
    public static void main(String[] args) {
    	   String parentLimt="578,579,580,581,";
    	   String newLimit ="574,575";
    	   System.out.println(parentLimt.contains("578"));
           //去掉一个某个字符 
    	  // StringBuffer parentLimtbuff = new StringBuffer(parentLimt);
    	   //parentLimtbuff.deleteCharAt(parentLimt.length()-1);
    	   //System.out.println(parentLimtbuff.toString());
    	   
	}
}
